package com.wwwc.util.web;

import java.util.*;

public class MyDateAndTime {    
    public static String getCurrentDate() {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	return((c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+"/"+c.get(Calendar.YEAR));
    }

    public static String getCurrentDate(int format) {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);

	String r = "";
	if(format==0) {
	    r = ((c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+"/"+c.get(Calendar.YEAR));
	}
	if(format==1) {
	    r = ((c.get(Calendar.MONTH)+1)+"_"+c.get(Calendar.DATE)+"_"+c.get(Calendar.YEAR));
	}
	if(format==3) {
	    r = (c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH)+1)+"-"+c.get(Calendar.DATE));
	}
	return r;
    }

    public static String getCurrentYear() {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	return(""+c.get(Calendar.YEAR));
    }

    public static int getCurrentYearInt() {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	//Calendar c = Calendar.getInstance();
	return(c.get(Calendar.YEAR));
    }

    public static String getCurrentMonth() {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	//Calendar c = Calendar.getInstance();
	return(""+(c.get(Calendar.MONTH)+1));
    }

    public static String getCurrentDay() {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	//Calendar c = Calendar.getInstance();
	return(""+c.get(Calendar.DATE));
    }

    public static String  getCurrentTime () {
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	//Calendar c = Calendar.getInstance();
	String am_pm = "";
	if ( c.get(Calendar.AM_PM)==1) {
	    am_pm = "PM";
	}
	else {
	    am_pm = "AM";
	}
	int h = c.get(Calendar.HOUR);
	int m = c.get(Calendar.MINUTE);
	int s = c.get(Calendar.SECOND);
	String hs = null;
	if(h<10) {
	    hs = "0"+h;
	}
	else {
	    hs =""+h;
	}
	String ms = null;
	if(m<10) {
	    ms = "0"+m;
	}
	else {
	    ms = ""+m;
	}
	String ss = null;
	if(s<10) {
	    ss = "0"+s;
	}
	else {
	    ss =""+s;
	}
	if(h==0 && am_pm.equals("PM")) {
	    hs = "12";
	}
	return (hs+":"+ ms+":"+ss+" "+am_pm);
    }

    public static String  getCurrentTime (int hour_format) {

	int hour = 0;
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	//Calendar c = Calendar.getInstance();

	if (hour_format == 24) {
	    if ( c.get(Calendar.AM_PM)==1) {
		hour = c.get(Calendar.HOUR)+12;
	    }
	    else {
		hour = c.get(Calendar.HOUR);
	    }
	}

	return (hour+":"+ c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND));
    }

    public static String getUserAge(String birthday) {
	int dm = 86400000; // [365*86400000 is too long will caruse problem]
	if(birthday == null || birthday.length() != 10) {
	    return "0";
	}

	int yyyy = Integer.parseInt(birthday.substring(0,4));
	int mm   = Integer.parseInt(birthday.substring(5,7));
	int dd   = Integer.parseInt(birthday.substring(8));
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar cc1 = Calendar.getInstance(tz, lc);
	Calendar cc2 = Calendar.getInstance(tz, lc);
	cc2.set(yyyy,(mm-1),dd, 0, 0);

	long life_time = (cc1.getTimeInMillis()-cc2.getTimeInMillis())/dm;
	return(""+life_time/365);
    }

    public static String getDailyDateList(int days) {
	int dm = 86400000;
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	long ct = c.getTimeInMillis(); 
  
	StringBuffer bf = new StringBuffer();
	String m = "";
	String d = "";
	String y = "";

	for(int n=0; n<days; n++) {
	    ct = ct + dm;
	    c.setTimeInMillis(ct);
	    m = "" + (c.get(Calendar.MONTH)+1);
	    if(m.length()==1) {
		m = "0" + m;
	    }
	    d = "" + c.get(Calendar.DATE);
	    if(d.length()==1) {
		d = "0" + d;
	    }
	    y = "" + c.get(Calendar.YEAR);
	    bf.append("<option value="+y+m+d+">"+m+"/"+d+"/"+y+"</option>");
	}

	return(bf.toString());
    }
    
    public static String getDailyDateList(int days, Vector v) {
	int dm = 86400000;
	Locale lc   = Locale.getDefault();
	TimeZone tz = TimeZone.getDefault();
	Calendar c = Calendar.getInstance(tz, lc);
	long ct = c.getTimeInMillis(); 
	Vector vr = new Vector();
	Enumeration enum1 = v.elements();
	while(enum1.hasMoreElements()) {
	    vr = (Vector)enum1.nextElement();
	}

	StringBuffer bf = new StringBuffer();
	String m = "";
	String d = "";
	String y = "";
	boolean add = true;
	long r1 = 0;
	long r2 = 0;

	for(int n=0; n<days; n++) {
	    add = true;
	    ct = ct + dm;
	    Enumeration enum11 = vr.elements();
	    while(enum11.hasMoreElements()) {
		r1 = Long.parseLong((String)enum11.nextElement());
		r2 = Long.parseLong((String)enum11.nextElement());
		if(ct >= r1 && ct <= r2) {
		    add = false;
		}
	    }

	    c.setTimeInMillis(ct);
	    m = "" + (c.get(Calendar.MONTH)+1);
	    if(m.length()==1) {
		m = "0" + m;
	    }

	    d = "" + c.get(Calendar.DATE);
	    if(d.length()==1) {
		d = "0" + d;
	    }

	    y = "" + c.get(Calendar.YEAR);
	    if(add) {
		bf.append("<option value="+y+m+d+">"+m+"/"+d+"/"+y+"</option>");
	    }
	    else {
		//bf.append("<option value=reserved>N/A</option>");
	    }
	}

	return(bf.toString());
    }

    public static String  formatDate (String dd_mm_yyyy) {
	String out_str = null;
	String year  = null;
	String month = null;
	String day   = null;

	String temp_date = dd_mm_yyyy;
	StringBuffer date_bf = new StringBuffer();
	temp_date = temp_date.toUpperCase();

	if (temp_date.indexOf("-") != -1 && temp_date.indexOf("-") != temp_date.lastIndexOf("-")) {
	    month = temp_date.substring(temp_date.indexOf("-")+1, temp_date.lastIndexOf("-"));
	    day   = temp_date.substring(0,temp_date.indexOf("-"));
	    year = temp_date.substring(temp_date.lastIndexOf("-")+1);

	    month = month.toUpperCase();

	    if (day.length()<2) { 
		day = "0"+day;
	    }
	    
	    if (month.startsWith("JAN")) {
		out_str = "01/"+day+"/"+year;
	    }
	    else if (month.startsWith("FEB") || month.equals("FEB")) {
		out_str = "02/"+day+"/"+year;
	    }
	    else if (month.startsWith("MAR") || month.equals("MAR")) {
		out_str = "03/"+day+"/"+year;
	    }
	    else if (month.startsWith("APR") || month.equals("APR")) {
		out_str = "04/"+day+"/"+year;
	    }
	    else if (month.startsWith("MAY") || month.equals("MAY")) {
		out_str = "05/"+day+"/"+year;
	    }
	    else if (month.startsWith("JUN") || month.equals("JUN")) {
		out_str = "06/"+day+"/"+year;
	    }
	    else if (month.startsWith("JUL") || month.equals("JUL")) {
		out_str = "07/"+day+"/"+year;
	    }
	    else if (month.startsWith("AUG") || month.equals("AUG")) {
		out_str = "08/"+day+"/"+year;
	    }
	    else if (month.startsWith("SEP") || month.equals("SEP")) {
		out_str = "09/"+day+"/"+year;
	    }
	    else if (month.startsWith("OCT") || month.equals("OCT")) {
		out_str = "10/"+day+"/"+year;
	    }
	    else if (month.equals("NOV") || month.equals("NOV")) {
		out_str = "11/"+day+"/"+year;
	    }
	    else if (month.startsWith("DEC") || month.equals("DEC")) {
		out_str = "12/"+day+"/"+year;
	    }
	    else {
		out_str = "ERROR:"+month;
	    }
	}
	else {
	    out_str = dd_mm_yyyy;
	}
	return (out_str);
    }
}
